﻿@{
	Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="" style="width:80%;margin:0 auto;">
	<div class="layui-form-item">
		<label class="layui-form-label">供应商编码</label>
		<div class="layui-input-block">
			<input type="hidden" id="Id" name="id" style="display:none">
			<input type="text" id="SupplierCode" name="SupplierCode" lay-verify="required" placeholder="请输入供应商编码" autocomplete="off" style="width:300px" class="layui-input">
			<input type="checkbox" lay-skin="switch" lay-filter="switchTest">自动生成
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">供应商名称</label>
		<div class="layui-input-block">
			<input type="text" id="SupplierName" name="SupplierName" lay-verify="required" placeholder="请输入供应商名称" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">供应商简称</label>
		<div class="layui-input-block">
			<input type="text" id="SupplierAbbreviation" name="SupplierAbbreviation" lay-verify="required" placeholder="请输入供应商简称" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">供应商等级</label>
		<div class="layui-input-block" style="width:350px">
			<select name="SupplierGrade" id="SupplierGrade" style="width:350px" lay-filter="aihao">
				<option selected></option>
				<option value="优质供应商">优质供应商</option>
				<option value="正常">正常</option>
				<option value="重点关注">重点关注</option>
				<option value="劣质供应商">劣质供应商</option>
				<option value="黑名单">黑名单</option>
			</select>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">供应商评分</label>
		<div class="layui-input-inline" style="width: 100px;">
			<input type="number" id="SupplierScore" name="SupplierScore" placeholder="" autocomplete="off" class="layui-input" min="0" step="1" lay-affix="number">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">供应商电话</label>
		<div class="layui-input-block">
			<input type="text" id="SupplierPhone" name="SupplierPhone" lay-verify="required" placeholder="请输入供应商电话" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">是否启用</label>
		<div class="layui-input-block">
			<input type="radio" name="State" value="true" title="是" checked>
			<input type="radio" name="State" value="false" title="否">
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-input-block">
			<button type="submit" class="layui-btn" lay-submit lay-filter="add">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			<button type="button" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
		</div>
	</div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>

<script>

	$(function () {
		getSupplierInfo()
	})

	function getSupplierInfo() {
		$.ajax({
			url: "/Supplier/GetSupplierInfo",
			data: { id: @ViewBag.id},
			type: "get",
			datatype: "json",
			success: function (res) {
				var id = res.data.id
				console.log(res.data)
				$("#Id").val(res.data.id);
				$("#SupplierCode").val(res.data.supplierCode)
				$("#SupplierName").val(res.data.supplierName)
				$("#SupplierAbbreviation").val(res.data.supplierAbbreviation)
				$("#SupplierGrade").val(res.data.supplierGrade)
				$("#SupplierScore").val(res.data.supplierScore)
				$("#SupplierPhone").val(res.data.supplierPhone)
				$("[name='State'][value=" + res.data.state + "]").prop("checked", true)

				layui.form.render("radio")
				layui.form.render("select")
			}
		})
	}

	//关闭弹框
	function closeWorkstation() {
		var index = parent.layer.getFrameIndex(window.name); //先得到当前的索引
		parent.layer.close(index); //再执行关闭
	}

	layui.use(['form', 'laydate', 'util'], function () {
		var form = layui.form;
		var layer = layui.layer;
		var laydate = layui.laydate;
		var util = layui.util;

		// 监听复选框变化，自动生成客户编码
		form.on('switch(switchTest)', function (data) {
			if (data.elem.checked) {
				// 生成随机字母+数字的编码并设置到CustomerCode输入框中
				var randomNum = Math.floor(Math.random() * 10) + '';
				for (var i = 0; i < 2; i++) {
					randomNum += Math.floor(Math.random() * 10);
				}
				randomNum = 'V00' + randomNum;
				$('input[name="SupplierCode"]').val(randomNum);

			}
			else {
				$('input[name="SupplierCode"]').val('');
			}
		});

		// 提交事件
		form.on('submit(add)', function (data) {
			var field = data.field; // 获取表单字段值

			console.log(field);
			// 发送 AJAX 请求添加数据
			$.ajax({
				url: '/Supplier/UpdateSupplier', // 替换成你的后端API添加接口URL
				type: 'POST', // 使用POST方法提交数据
				data: field, // 发送表单字段数据
				dataType: 'json', // 期望从服务器返回的数据类型
				success: function (res) {
					//alert(3333333);
					// 添加成功后的处理
					if (res > 0) { // 假设后端返回的成功状态码为0
						layer.alert('修改成功', { title: '操作结果' }, function () {
							// 添加成功后的逻辑，例如关闭弹窗，刷新表格等
							layer.closeAll(); // 关闭所有弹窗
							// 这里可以刷新表格，重新加载数据等
							closeWorkstation()
						});
					}
					else if(res == -1){
						layer.alert('名称重复');
							return;
					}
					else {
						layer.alert('修改失败：' + res.msg, { title: '操作结果' });
					}
				},
				error: function (err) {
					// 处理添加失败的情况
					layer.alert('修改失败：网络异常，请稍后重试', { title: '操作结果' });
					console.error('修改失败', err);
				}
			});

			return false; // 阻止默认 form 提交
		});
	});
</script>

